package top.kaoshanji.p1ch06a;

// import Acme.JPM.Encoders.GifEncoder;
import org.apache.hc.core5.http.ContentType;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

/**
 * 代码清单-p1ch06a-187页a：Hello World图像
 * URL访问地址：/helloWorldGraphics
 * @author kaoshanji
 * ^_^
 * create time 2025/4/4 17:41
 */
public class HelloWorldGraphics extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 二进制输出
        ServletOutputStream out = resp.getOutputStream();

        Frame frame = null;
        Graphics g = null;

        try {
            // 生成一个不显示的 Frame
            frame = new Frame();
            frame.addNotify();

            // 使用 Frame 取得一个图形区域
            Image image = frame.createImage(400, 60);
            g = image.getGraphics();

            // 在幕后图形环境中画“Hello World!”
            g.setFont(new Font("Serif", Font.ITALIC, 48));
            g.drawString("Hello World!", 10, 50);

            // 将幕后图像编码到一个GIF中并发送到客户端
            resp.setContentType(ContentType.IMAGE_GIF.getMimeType());
          //  GifEncoder encoder = new GifEncoder(image, out);
          //  encoder.encode();

        } finally {
            // 清除资源
            if (g != null) {
                g.dispose();
            }

            if (frame != null) {
                frame.removeNotify();
            }

        }


    }


}
